package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParm;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 1 学生分页·查询
     */
    @Override
    public PageResult<Student> page(StudentQueryParm studentQueryParm) {
        //1.设置分页查询参数
        PageHelper.startPage(studentQueryParm.getPage(), studentQueryParm.getPageSize());
        //2 执行查询
        List<Student> studentList = studentMapper.page(studentQueryParm);
        //3 解析结果封装PageResult对象并返回
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<Student>(p.getTotal(), p.getResult());
    }
    /**
     * 2 新增学生
     */

    @Override
    public void add(Student student) {
        //1.补全参数
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);

    }

    /**
     * 3 根据id查询学生
     */
    @Override
    public Student findById(Integer id) {
       Student student= studentMapper.findById(id);
       return student;
    }


    /**
     * 4 修改学生
     */
    @Override
    public void update(Student student) {
        //1.补全参数
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }
    /**
     * 5 删除学生
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);

    }
    /**
     * 6 违纪处理
     */
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id,score);

    }


}

